package com.sjtu.vfact.visualization.controller;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.swing.DefaultListCellRenderer;
import javax.swing.JList;

import com.sjtu.vfact.MainController;
import com.sjtu.vfact.textual.cluster.FTCClusterer;
import com.sjtu.vfact.textual.model.Cluster;
import com.sjtu.vfact.textual.model.Document;
import com.sjtu.vfact.textual.model.MasterTermset;
import com.sjtu.vfact.textual.model.TermSet;
import com.sjtu.vfact.visualization.base.LAYOUT;
import com.sjtu.vfact.visualization.base.VfactEdge;
import com.sjtu.vfact.visualization.base.VfactVertex;
import com.sjtu.vfact.visualization.util.VizUtil;
import com.sjtu.vfact.visualization.view.GraphFrame;
import com.sjtu.vfact.visualization.view.GraphPanel;

import edu.uci.ics.jung.algorithms.layout.Layout;
import edu.uci.ics.jung.graph.Graph;

public class GraphController {
	
	private MainController main;
	
	private GraphFrame frame;
	private GraphPanel graphPanel;
	
	public GraphController(MainController main){
		this.main = main;
	}
	
	public void start(List<MasterTermset> vertices){
		frame = new GraphFrame();
		graphPanel = new GraphPanel();
		graphPanel.setupView(vertices);
		frame.generateGraph(graphPanel);
		frame.displayFrame(true);
		addListeners();
//		setRenderers();
	}
	
	public void startSSAGraph(Map<TermSet, Set<Document>> ssaDict){
		frame = new GraphFrame();
		graphPanel = new GraphPanel();
		graphPanel.setupSSAView(ssaDict);
		frame.generateSSAGraph(graphPanel);
		frame.displayFrame(true);
		addListeners();
//		setRenderers();
	}
	
	public void addListeners(){
		frame.addBtnClusterListener(new BtnClusterListener());
		frame.addBtnUnclusterListener(new BtnUnclusterListener());
		frame.addRdBtnAnimationOnListener(new RdBtnAnimationOnListener());
		frame.addRdBtnAnimationOffListener(new RdBtnAnimationOffListener());
		frame.addCmbBoxLayoutListener(new CmbBoxLayoutListener());
		frame.addCmbBoxClusterLayoutListener(new CmbBoxClusterLayoutListener());
	}
	
//	public void setRenderers(){
//		frame.setCmbBoxLayoutRenderer(new CmbBoxLayoutRenderer());
//		frame.setCmbBoxClusterLayoutRenderer(new CmbBoxLayoutRenderer());
//	}
	
	public void clusterGraph(List<Cluster<VfactVertex>> clusters){
		graphPanel.clusterGraph(clusters);
	}
	
	public void unclusterGraph(){
		graphPanel.unclusterGraph();
	}
	
	public Graph<VfactVertex, VfactEdge> getGraph(){
		return graphPanel.getGraph();
	}
	
	public List<VfactVertex> getVertices(){
		return new ArrayList<VfactVertex>(graphPanel.getGraph().getVertices());
	}
	
	class BtnClusterListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			unclusterGraph();
			FTCClusterer clusterer = new FTCClusterer();
			List<Cluster<VfactVertex>> clusters = clusterer.transform(getVertices(), 0.6, 4);
			clusterGraph(clusters);
		}
		
	}
	
	class BtnUnclusterListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent arg0) {
			unclusterGraph();
		}
		
	}
	
	class RdBtnAnimationOnListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			
		}
		
	}
	
	class RdBtnAnimationOffListener implements ActionListener{

		@Override
		public void actionPerformed(ActionEvent e) {
			
		}
		
	}
	
	class CmbBoxLayoutListener implements ItemListener{

		@Override
		public void itemStateChanged(ItemEvent e) {
			if(e.getStateChange() == ItemEvent.SELECTED){
//				Class<?> cl = (Class<?>) e.getItem();
				String layoutName = (String) e.getItem();
//				Class<?> cl = LAYOUT.getLayoutByName(layoutName).getLayoutClass();
				try{
//					Layout<VfactVertex, VfactEdge> layout = graphPanel.getLayoutFor(cl, graphPanel.getGraph());
					Layout<VfactVertex, VfactEdge> layout = VizUtil.createLayoutByName(layoutName, graphPanel.getGraph());
					layout.setInitializer(graphPanel.getViewer().getGraphLayout());
					graphPanel.getLayout().setDelegate(layout);
					graphPanel.getViewer().setGraphLayout(graphPanel.getLayout());
				}catch(Exception ex){
					ex.printStackTrace();
				}
			}
		}
	}
	
	class CmbBoxClusterLayoutListener implements ItemListener{

		@Override
		public void itemStateChanged(ItemEvent e) {
			if(e.getStateChange() == ItemEvent.SELECTED){
				String layoutName = (String) e.getItem();
//				Class<?> cl = LAYOUT.getLayoutByName(layoutName).getLayoutClass();
//				graphPanel.setClusteringLayoutClass(cl);
				graphPanel.setClusteringLayout(layoutName);
			}
		}
	}
	
//	class CmbBoxLayoutRenderer extends DefaultListCellRenderer{
//
//		private static final long serialVersionUID = 1L;
//
//		public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
//             String valueString = value.toString();
//             valueString = valueString.substring(valueString.lastIndexOf('.')+1);
//             return super.getListCellRendererComponent(list, valueString, index, isSelected,
//                     cellHasFocus);
//         }
//		
//	}
}
